using System;
using System.Reflection;


namespace FrameworkDesign.Example
{
    public class Singleton<T> where T : Singleton<T>
    {
        private static T _instance;
        public static T instance
        {
            get
            {
                if (_instance == null)
                {
                    var ctors = typeof(T).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic);
                    var ctor = Array.Find(ctors, c => c.GetParameters().Length == 0);
                    if (ctor == null)
                        throw new Exception("Ctor is Empty");
                    _instance = (T)ctor.Invoke(null);
                }
                return _instance;
            }
        }
    }
}


